package com.example.nccdeploytool.component;

/**
 * @author 15037
 * @description
 * @date 2025-04-27 9:35
 */
import javafx.animation.*;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class BubbleToast {

    public static void show(Stage owner, String message) {
        // 创建透明舞台
        Stage toastStage = new Stage();
        toastStage.initOwner(owner);
        toastStage.initStyle(StageStyle.TRANSPARENT);

        // 创建内容
        Label label = new Label(message);
        label.setStyle("-fx-text-fill: white; -fx-font-size: 14px;");

        StackPane root = new StackPane(label);
        root.setStyle("-fx-background-color: rgba(0,0,0,0.7); -fx-background-radius: 20; -fx-padding: 10 20 10 20;");
        root.setOpacity(0);

        // 创建冒泡效果
        createBubbleEffect(root);

        Scene scene = new Scene(root);
        scene.setFill(Color.TRANSPARENT);
        toastStage.setScene(scene);
        toastStage.show();

        // 显示动画
        FadeTransition fadeIn = new FadeTransition(Duration.millis(300), root);
        fadeIn.setToValue(1);

        // 几秒后自动消失
        PauseTransition delay = new PauseTransition(Duration.seconds(2));
        FadeTransition fadeOut = new FadeTransition(Duration.millis(300), root);
        fadeOut.setToValue(0);

        SequentialTransition seqTransition = new SequentialTransition(
                fadeIn, delay, fadeOut
        );
        seqTransition.setOnFinished(e -> toastStage.close());
        seqTransition.play();

        // 定位到主窗口底部中央
        toastStage.setX(owner.getX() + owner.getWidth()/2 - root.getWidth()/2);
        toastStage.setY(owner.getY() + owner.getHeight() - 100);
    }

    private static void createBubbleEffect(StackPane pane) {
        // 创建气泡
        Circle bubble1 = new Circle(5, Color.WHITE);
        Circle bubble2 = new Circle(3, Color.WHITE);

        bubble1.setOpacity(0);
        bubble2.setOpacity(0);

        pane.getChildren().addAll(bubble1, bubble2);

        // 气泡动画
        Timeline bubbleAnim = new Timeline(
                new KeyFrame(Duration.ZERO,
                        new KeyValue(bubble1.translateYProperty(), 0),
                        new KeyValue(bubble2.translateYProperty(), 0),
                        new KeyValue(bubble1.opacityProperty(), 0.7),
                        new KeyValue(bubble2.opacityProperty(), 0.5)
                ),
                new KeyFrame(Duration.millis(800),
                        new KeyValue(bubble1.translateYProperty(), -15),
                        new KeyValue(bubble2.translateYProperty(), -20),
                        new KeyValue(bubble1.opacityProperty(), 0),
                        new KeyValue(bubble2.opacityProperty(), 0)
                )
        );

        bubbleAnim.setCycleCount(Animation.INDEFINITE);
        bubbleAnim.play();
    }
}
